#include "bileton.h"

using namespace std;

int bilet::total = 0;
ifstream &operator>>(ifstream &in, bilet &bilet)
{
  char buf[BUF_L];
  char end;
  char *bufName = nullptr;
  int bufDiam;
  bool bufLife;
  int bufSat;
  if (in.getline(buf, BUF_L, '\t') && in >> bufDiam && in >> bufLife && in >> bufSat && in.get(end))
  {
    int l_name = strlen(buf) + 1;
    bufName = new char[l_name];
    strncpy(bufName, buf, l_name);
    bilet bufbilet(bufName, bufDiam, bufLife, bufSat);
    bilet = bufbilet;
  }
  delete[] bufName;
  return in;
}

ofstream &operator<<(ofstream &out, bilet &bilet)
{
  out << bilet.getName() << '\t' << bilet.getDiam() << ' ' << bilet.getLife() << ' ' << bilet.getSat() << endl;
  return out;
}

void read_database(const char *&filename, bilet *&bilets, int &bilets_count)
{
  ifstream in(filename, ios::in);
  if (!in.is_open())
  {
    cout << "Ошибка открытия файла" << endl;
    return;
  }
  bilet *state_bilets;
  bilet new_bilet;
  while (in >> new_bilet)
  {
    ++bilets_count;
    state_bilets = new bilet[bilets_count];
    for (int i = 0; i < bilets_count - 1; ++i)
    {
      state_bilets[i] = bilets[i];
    }
    delete[] bilets;
    state_bilets[bilets_count - 1] = new_bilet;
    bilets = state_bilets;
  }
  in.close();
}

void write_database(const char *&filename, bilet *&bilets, int &bilets_count)
{
  ofstream out(filename, ios::out);
  if (!out.is_open())
  {
    cout << "Не удаётся создать файл" << endl;
    return;
  }
  for (int i = 0; i < bilets_count; ++i)
  {
    out << bilets[i];
  }
  cout << "Поезд сохранен в файл" << endl;
  out.close();
}

void sort_database(bilet *&bilets, const int &bilets_number)
{
  for (int i = 0; i + 1 < bilets_number; ++i)
  {
    for (int j = 0; j + 1 < bilets_number - i; ++j)
    {
      if (bilets[j] > bilets[i + 1])
      {
        bilet bufbilet = bilets[i];
        bilets[j] = bilets[j + 1];
        bilets[j + 1] = bufbilet;
      }
    }
  }
}
void add(bilet *&bilets, int &bilets_count)
{
  char buf[BUF_L];
  int bufNVag;
  bool bufKupe;
  int bufPlacet;
  cout << "Введите имя\n";
  cin >> buf;
  int l_name = strlen(buf) + 1;
  char *bufName = new char[l_name];
  strncpy(bufName, buf, l_name);
  cout << "Введите вагон\n";
  cin >> bufNVag;
  cout << "Нажмите 1, если купе. 0, если нет\n";
  cin >> bufKupe;
  cout << "Укажите место\n";
  cin >> bufPlace;
  bilet new_bilet(bufName, bufNVag, bufKupe, bufPlace);
  ++bilets_count;
  bilet *bilets_state = new bilet[bilets_count];
  for (int i = 0; i < bilets_count - 1; ++i)
  {
    bilets_state[i] = bilets[i];
  }
  delete[] bilets;
  bilets_state[bilets_count - 1] = new_bilet;
  bilets = bilets_state;
  delete[] bufName;
}

void remove(bilet *&bilets, int &bilets_count, const int &id)
{
  bilets_count--;
  cout << "Билет " << bilets[id].getName() << " больше не действителен." << endl;
  for (int i = id; i < bilets_count; ++i)
  {
    bilets[i] = bilets[i + 1];
  }
  bilet *state_bilets = new bilet[bilets_count];
  for (int i = 0; i < bilets_count; ++i)
  {
    state_bilets[i] = bilets[i];
  }
  delete[] bilets;
  bilets = state_bilets; //
}

void print_database(bilet *&bilets, const int &bilets_count)
{
  if (bilets_count == 0)
  {
    cout << "Ничего нет" << endl;
    return;
  }
  for (int i = 0; i < bilets_count; ++i)
  {
    bilets[i].print();
  }
  cout << endl;
}

int find(bilet *&bilets, const int &bilets_count)
{
  cout << "Введите название пассажира" << endl;
  string input;
  cin >> input;
  int ind = -1;
  for (int i = 0; i < bilets_count; i++)
  {
    if (bilets[i].getName() == input)
    {
      ind = i;
      bilets[i].print();
      cout << endl;
      return i;
    }
  }
  cout << "Нет такой буквы)" << endl;
  return ind;
}

int main()
{
    {
  int integer;
  cout << endl;
  cout << "Введите число от 1 до 7: ";

  if (cin >> integer)
  {
  }
  else
  {
    cin.clear();
    cin.ignore(1000, '\n');
  }

  return integer;
}
}